<?php
class foreground_pc_default_common_action extends wwy_action
{
    protected $_view_dir = PROJECT_ROOT_PATH . '/no_public/module/foreground/pc/view/view';
    protected $_library_dir = PROJECT_ROOT_PATH . '/no_public/module/foreground/pc/view/library';
    protected $_layout_dir = PROJECT_ROOT_PATH . '/no_public/module/foreground/pc/view/layout';

    protected $_layout = 'common/layout.php';
    
    protected  function _pre_run()
    {
        parent::_pre_run();
    
        // !! 有可能这些数据，是根据【不同平台】，在【不同平台】的【controller】初始化，然后再传入【controller_server】的
        $session_member_info = array('id' => null, 'mobile' => null, 'default_address_id' => null);
        $member_info = array();
        if(!empty($_SESSION['member_id'])){
            $session_member_info['id']                 = $_SESSION['member_id'];
            $session_member_info['mobile']             = $_SESSION['member_mobile'];
            $session_member_info['default_address_id'] = $_SESSION['member_default_address_id'];
    
    
            $model_foreground_member = wwy_resource_container::get_model_foreground_member();
            $member_info = $model_foreground_member->find($session_member_info['id']);
        }
        
        self::$data['session_member_info'] = $session_member_info;
        self::$data['member_info'] = $member_info;
    
        $this->_set_member_view_data();        
    }
    
    protected function _set_member_view_data()
    {
        self::$_view_data['session_member_info'] = self::$data['session_member_info'];
        self::$_view_data['member_info'] = self::$data['member_info'];
    }
    
    
    
    /**
     * 渲染布局、合成整个页面，并输出
     */
    protected function _render()
    {
        //var_dump(__METHOD__); exit();
                
        //渲染布局中使用到的库
        $cat_tree_all_output = $this->_render_cat_tree_all();
        //var_dump($cat_tree_all_output); exit();
        //echo $cat_tree_all_output; exit();
        self::$_layout_data['library_footer'] = $this->_render_library('common/footer.php');
    
        //页面标题
        $this->_set_page_title();
        self::$_view_data['this_year'] = date('Y');
    
        $layout_content = $this->_render_layout($this->_layout);
        echo $layout_content;
    }
    
    /**
     * 对应页面必须设置【self::$_view_data['page_title']】，用于生成【self::$_layout_data['page_title']】
     */
    protected function _set_page_title()
    {
        $page_title = self::$_view_data['business_config']['title'] . ' - 前台';
    
        if(!empty(self::$_view_data['page_title'])){
            $page_title = self::$_view_data['page_title'] . ' | ' . $page_title;
        }
    
        self::$_layout_data['page_title'] = $page_title;
    }
    
    protected function _render_cat_tree_all()
    {
        $all_category_tree   = self::$_view_data['all_category_tree'];
        $cat_tree_output = $this->_render_cat_tree($all_category_tree);
    
        $cat_tree_all_output = $cat_tree_output;
        self::$_layout_data['cat_tree_all'] = $cat_tree_output;
    
        return $cat_tree_all_output;
    }
    
    /**
     * 渲染【非循环】的【外部结构：ul】
     *
     * 此方法比较【完善】, 拆分成【两个】方法
     *     1)一个渲染【非循环】的【外部结构：ul】
     *     2)一个渲染【循环】的【列表结构：li】
     */
    protected function _render_cat_tree($category_tree, $level = 1)
    {
        $cat_tree_output = '';
        if(!empty($category_tree)){
            $cat_tree_li_output = $this->_render_cat_tree_list($category_tree, $level);
    
            ob_start();
            require $this->_library_dir . '/common/cat-tree-ul.php';
            $cat_tree_output = ob_get_clean();
            
        }
    
        return $cat_tree_output;
    }
    
    /**
     * 渲染【循环】的【列表结构：li】
     *
     * 此方法比较【完善】, 拆分成【两个】方法
     *     1)一个渲染【非循环】的【外部结构：ul】
     *     2)一个渲染【循环】的【列表结构：li】
     */
    public function _render_cat_tree_list($category_tree, $level = 1)
    {
        $cat_tree_li_output = '';
        $next_level = $level + 1;
        foreach($category_tree as $cat){
            $cat_info = $cat['info'];    //@by gbfun 【渲染】用到
    
            $category_tree_children = $cat['children'];
            $cat_tree_children_output = $this->_render_cat_tree($category_tree_children, $next_level);
    
            ob_start();
            require $this->_library_dir . '/common/cat-tree-li.php';
            $cat_output = ob_get_clean();
    
            $cat_tree_li_output .= $cat_output;
        }
    
        return $cat_tree_li_output;
    }
}